package patrol.mobile.com.mobilepatrol.activity.track;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.dl7.recycler.listener.OnRecyclerViewItemLongClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.TrackListDetailAdapter;
import patrol.mobile.com.mobilepatrol.bean.PatrolDetailInfoBean;
import patrol.mobile.com.mobilepatrol.manager.TrackManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.widget.dialog.AppleDialog;

import static patrol.mobile.com.mobilepatrol.R.mipmap.add;

/**
 * Created by apple on 17/11/7.
 */

public class TrackDetailListActivity extends BaseActivity {
    @BindView(R.id.recycler)
    RecyclerView recyclerView;

    private TrackListDetailAdapter mAdapter;
    private List<PatrolDetailInfoBean> mList = new ArrayList<>();
    private String mID;

    @Override
    protected int attachLayoutRes() {
        return R.layout.comment_list;
    }

    @Override
    public void setListener() {
        mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent = new Intent(TrackDetailListActivity.this,TrackDetailActivity.class);
                intent.putExtra("ID",mID);
                intent.putExtra("detailID",mList.get(position).ID);
                intent.putExtra("alter",true);
                startActivity(intent);
                CommonUtil.openNewActivityAnim(TrackDetailListActivity.this,false);
            }
        });
        mAdapter.setOnItemLongClickListener(new OnRecyclerViewItemLongClickListener() {
            @Override
            public boolean onItemLongClick(View view, final int position) {
                final AppleDialog dialog = new AppleDialog(TrackDetailListActivity.this,R.style.Dialog,"确认删除？");
                dialog.show();
                dialog.setmListener(new AppleDialog.SureListener() {
                    @Override
                    public void onSure() {
                        String patrolId=mList.get(position).patrolID;
                        TrackManager.delPatrolDetail(mList.get(position).ID);
                        mList = TrackManager.getAllDetailList(patrolId);
                        mAdapter.updateItems(mList);
                        dialog.dismiss();
                    }
                });

                return false;
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mList = TrackManager.getAllDetailList(mID);
        mAdapter.updateItems(mList);
    }
    @Override
    public void initData() {
        mID = getIntent().getStringExtra("ID");
        mList = TrackManager.getAllDetailList(mID);
        mAdapter.updateItems(mList);
        recyclerView.setAdapter(mAdapter);
    }
    @Override
    public void initView() {
        super.initView();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new TrackListDetailAdapter(this, mList);
    }

    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(TrackDetailListActivity.this,true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("巡道管理明细列表");
        //增加明细
        iv_right_title.setVisibility(View.VISIBLE);
        iv_right_title.setImageResource(add);
        iv_right_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(TrackDetailListActivity.this,TrackDetailActivity.class);
                intent.putExtra("ID",mID);
                intent.putExtra("alter",false);
                startActivity(intent);
                CommonUtil.openNewActivityAnim(TrackDetailListActivity.this,false);

            }
        });
    }
    public void upload(String id){

    }
}
